//
//  JiaoHuShiViewController.m
//  动画
//
//  Created by 张国文 on 16/9/13.
//  Copyright © 2016年 李国峰. All rights reserved.
//

#import "JiaoHuShiViewController.h"
#import "CustomModelAnimaton.h"
#import "TargetViewController.h"

@interface JiaoHuShiViewController ()
{
    UIPercentDrivenInteractiveTransition* interactionController;
}

@end

@implementation JiaoHuShiViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didClickPanGestureRecognizer:)];
    
    [self.navigationController.view addGestureRecognizer:panRecognizer];
    
}

- (void)didClickPanGestureRecognizer:(UIPanGestureRecognizer*)recognizer

{
    
    UIView* view = self.view;
    
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        
        interactionController = [[UIPercentDrivenInteractiveTransition alloc] init];
        TargetViewController *targetVC = [[TargetViewController alloc] init];
        targetVC.transitioningDelegate = self;
        [self presentViewController:targetVC animated:YES completion:nil];
        
    } else if (recognizer.state == UIGestureRecognizerStateChanged) {
        
        CGPoint translation = [recognizer translationInView:view];
        
        CGFloat distance = fabs(translation.x / CGRectGetWidth(view.bounds));
        
        [interactionController updateInteractiveTransition:distance];
        
    } else if (recognizer.state == UIGestureRecognizerStateEnded) {
        
        CGPoint translation = [recognizer translationInView:view];
        
        CGFloat distance = fabs(translation.x / CGRectGetWidth(view.bounds));
        
        if (distance > 0.5) {
            
            [interactionController finishInteractiveTransition];
            
        } else {
            
            [interactionController cancelInteractiveTransition];
            
        }
        
        interactionController = nil;
        
    }
    
}

- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id<UIViewControllerAnimatedTransitioning>)animator
{
    return interactionController;
}

- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator
{
    return nil;
}


@end
